using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using WebApplication1.Core.Domain.RepositoryContracts;
using WebApplication1.Core.ServiceContracts;
using WebApplication1.Core.Services;
using WebApplication1.Infrastructure.DatabaseContext;
using WebApplication1.Infrastructure.Repositories;

namespace WebApplication1.WebApi {
    public class Program {
        public static void Main(string[] args) {
;
            var builder = WebApplication.CreateBuilder(args);

            //add myapp services to the container
            registerMyAppService(builder);
            //add controller to the service contanier
            builder.Services.AddControllers();
            //add Cors to the contianer
            var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
            builder.Services.AddCors(options => {
                options.AddPolicy(name: MyAllowSpecificOrigins, policy => {
                    policy.WithOrigins("http://localhost:4200", "https://localhost:4200")
                          .AllowAnyMethod()
                          .AllowAnyHeader();
                });
            });
            //add swagger
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            //set up authentication middleware
            var secretkey = builder.Configuration.GetValue<string>("SecretKey");
            builder.Services.AddAuthentication(option => {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options => {
                options.TokenValidationParameters = new TokenValidationParameters() {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretkey ?? string.Empty)),
                    ValidateLifetime = true,
                    ValidateAudience = false,
                    ValidateIssuer = false,
                    ClockSkew = TimeSpan.Zero
                };
            });
            var app = builder.Build();

            //configure swagger
            if(app.Environment.IsDevelopment()) {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
            #region Configure the HTTP request pipeline.
            //enable https
            app.UseHttpsRedirection();
            app.UseCors(MyAllowSpecificOrigins);
            app.UseAuthentication();
            app.UseAuthorization();
            app.MapControllers();
            app.MapStaticAssets();
            #endregion
            app.Run();
        }

        static void registerMyAppService(WebApplicationBuilder builder) {
            // add DbContext to the service container.
            builder.Services.AddDbContext<SQLiteDbContext>(options => {
                options.UseSqlite(builder.Configuration.GetConnectionString("SQLiteConnection"));
            });
            //add Data Access Object to the container
            builder.Services.AddScoped<ISheQuRepository, SheQuRepository>();
            builder.Services.AddScoped<ILouYuanRepository, LouYuanRepository>();
            builder.Services.AddScoped<IXiangxixinxiRepository, XiangxixinxiRepository>();


            //add Business Logic Object to the service container
            builder.Services.AddScoped<ISheQuService, SheQuService>();
            builder.Services.AddScoped<ILouYuanService, LouYuanService>();
            builder.Services.AddScoped<IXiangxixinxiService, XiangxixinxiService>();

        }
    }
}
